<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Controller;
use Blovex4\Logic\AdminLogic;

/**
 * 菜单配置控制器
 *
 * @author derek <693472824@qq.com>
 */
class MenuController extends BaseController{

    /**
     * 菜单配置控制器首页
     *
     * @author derek <693472824@qq.com>
     */
    public function index () {
        
        $menu_tree = D('Menu', 'Logic')->getMenuTree();
        $this->assign('__AUTH__', $menu_tree);
        $this->display();
    }

    /**
     * 增加菜单以及修改菜单
     *
     * @param integer $menu_id
     *            菜单ID.
     * @author derek <693472824@qq.com>
     */
    public function editAction () {
        
        $menu_id = I('menu_id');
        $menu = D('Menu', 'Logic');
        $menu_tree = $menu->getMenuTree();
        
        if (! empty($menu_id)) {
            
            $menuModel = $menu->getMenuByID($menu_id);
            
            if (empty($menuModel)) {
                $this->error('获取数据失败', U('Menu/index'));
            } else {
                $this->assign('menuModel', $menuModel);
            }
        }
        
        $this->assign('menu_tree', $menu_tree);
        $this->display();
    }

    /**
     * 菜单新增
     *
     * @author derek <693472824@qq.com>
     */
    public function addMenu() {
        
        $menuLogic = D('Menu', 'Logic');
        $result = $menuLogic->addMenu();
        
        if ($result === false) {
            $this->error('操作失败,' . $menuLogic->getError());
        } else {
            
            $adminid = AdminLogic::is_login();
            session('ADMIN_MENU' . $adminid, null);
           
            $this->success('操作成功', U('Menu/index'));
        }
    }

    /**
     * 菜单删除
     *
     * @param array|integer $menu_id
     *            菜单ID
     * @return boolean true:代表成功 flase:代表失败
     * @author derek <693472824@qq.com>
     */
    public function del() {
        
        $menu_id = I('menu_id');
        
        if (empty($menu_id)) {
            $this->error('参数错误');
        }
        
        $menu = D('Menu', 'Logic');
        $result = $menu->del();
        
        if ($result === false) {
            $this->error('操作失败', U('index'));
        } else {
            $adminid = AdminLogic::is_login();
            session('ADMIN_MENU' . $adminid, null);
            $this->success('操作成功！', U('index'));
        }
    }

    /**
     * 菜单规则设置，获取菜单规则列表
     *
     * @param array|integer $menu_id
     *            菜单ID
     * @author derek <693472824@qq.com>
     */
    public function menuRuleList ()
    {
        $menu_id = I('menu_id');
        if(empty($menu_id))
        {
        	$this->error('参数错误',U('Menu/index'));
        }
        $menu_rule = M('Menu_rule');
        $menu_rule_list = $menu_rule->where('menu_id=' . $menu_id)->order('rule_id desc')->select();
        $menu_name = M('Menu')->where('menu_id=' . $menu_id)->getField(
                'menu_name');
        $this->assign('rule_list', $menu_rule_list);
        $this->assign('menu_name', $menu_name);
        $this->assign('menu_id', $menu_id);
        $this->display();
    }

    /**
     * 菜单规则新增
     *
     * @param int|string $menu_id
     *            菜单ID
     * @author derek <693472824@qq.com>
     */
    public function addMenuRule ()
    {
        $menu_id = I('menu_id');
        if(empty($menu_id))
        {
            $this->error('参数错误',U('Menu/index'));
        }
        $menuRuleLogic = D('MenuRule', 'Logic');
        $result = $menuRuleLogic->addMenuRule();
        
        if ($result === false) {
            $this->error('操作失败,' . $menuRuleLogic->getError());
        } else {
            $adminid = AdminLogic::is_login();
            session('ADMIN_MENU' . $adminid, null);
            $this->success('操作成功', U('Menu/menuRuleList?menu_id=' . $menu_id));
        }
    }

    /**
     * 增加菜单规则以及修改菜单规则
     *
     * @param integer $menu_id
     *            菜单ID.
     * @param
     *            menuRule_id 菜单规则ID
     * @author derek <693472824@qq.com>
     */
    public function editMenuRule ()
    {
        $menu_id = I('menu_id');
        $menuRule_id = I('menuRule_id');
        $menuRule = D('MenuRule', 'Logic');
        $menu_name = M('Menu')->where('menu_id=' . $menu_id)->getField(
                'menu_name');
        if(empty($menu_id))
        {
            $this->error('参数错误',U('Menu/index'));
        }
        if (! empty($menuRule_id)) {
            
            $menuRuleModel = $menuRule->getMenuRuleByID($menuRule_id);
            if (empty($menuRuleModel)) {
                $this->error('获取数据失败', U('Menu/menuRuleList'));
            } else {
                $this->assign('menuRuleModel', $menuRuleModel);
            }
        }
        $this->assign('menu_name', $menu_name);
        $this->assign('menu_id', $menu_id);
        $this->display();
    }

    /**
     * 菜单规则删除
     *
     * @param array|integer $menu_id
     *            菜单ID
     * @return boolean true:代表成功 flase:代表失败
     * @author derek <693472824@qq.com>
     */
    public function del_rule ()
    {
        $menuRule_id = I('rule_id');
        if (empty($menuRule_id)) {
            $this->error('参数错误');
        }
        $menuRule = D('MenuRule', 'Logic');
        $result = $menuRule->del();
        if ($result === false) {
            $this->error('操作失败');
        } else {
            $adminid = AdminLogic::is_login();
            session('ADMIN_MENU' . $adminid, null);
            $this->success('操作成功');
        }
    }
}